package models

import (
	"log"
	"time"
	"xorm.io/builder"
)

type Reported struct {
	Id                int       `xorm:"not null pk autoincr INT(11)"`
	StudentId         int       `xorm:"index INT(11)"`
	SchoolId          int       `xorm:"index INT(11)"`
	Subject           string    `xorm:"comment('科目') index ENUM('1','2','3','4')"`
	Validetime        int64     `xorm:"INT(11)"`
	Result            string    `xorm:"comment('0不合格1合格2审核中') ENUM('0','1','2')"`
	Needtime          int64     `xorm:"default 0 comment('需要补充的学时') INT(11)"`
	Needmileage       float64   `xorm:"default 0 comment('需要补学公里数') FLOAT"`
	Reason            string    `xorm:"default '' comment('不合格的原因') VARCHAR(255)"`
	Checktime         time.Time `xorm:"index DATETIME"`
	Validmileage      float32   `xorm:"default 0 comment('有效公里数') FLOAT"`
	Isreport          string    `xorm:"comment('0未上报1已上报') ENUM('0','1')"`
	PdfId             int64     `xorm:"comment('电子培训部分记录表PDF文件ID') index INT(11)"`
	CheckStatus       string    `xorm:"comment('0不合格1合格') ENUM('0','1')"`
	RecheckStatus     string    `xorm:"comment('0不合格1合格') ENUM('0','1')"`
	NeedNetworktime   int       `xorm:"default 0 comment('需要远程的学时') INT(11)"`
	NeedClasstime     int       `xorm:"default 0 comment('需要课堂的学时') INT(11)"`
	NeedVehicletime   int       `xorm:"default 0 comment('需要实车的学时') INT(11)"`
	NeedSimulatortime int       `xorm:"default 0 comment('需要模拟器的学时') INT(11)"`
	UpdateTime        time.Time `xorm:"index DATETIME"`
	Duration          int64     `xorm:"not null comment('审核合格学时') INT(11)"`
	TotalInvalidTime  int64     `xorm:"not null comment('本培训部分本次的审核不合格总学时，n分钟') INT(11)"`
}

type ReportList struct {
	Id              int    `json:"id"`
	StudentId       string `json:"student_id"`
	PdfId           string `json:"pdf_id"`
	UpdateTime      string `json:"update_time"`
	Subject         string `json:"subject"`
	Name            string `json:"name"`
	SchoolName      string `json:"school_name"`
	Phone           string `json:"phone"`
	IdNumber        string `json:"id_number"`
	TrainingCarType string `json:"training_car_type"`
	OperationStatus string `json:"operation_status"`
}

func (r *Reported) GetReportedList(filter builder.Cond, pageSize, offset int) (list []ReportList, count int64, err error) {
	err = engine.Table("dts_reported").Alias("r").
		Select("r.pdf_id,r.update_time, st.id as student_id,s.name_short as school_name,s.id as school_id,r.id,u.name,r.pdf_id,u.phone,u.id_number,r.subject,st.operation_status,st.training_car_type,s.province,s.city").
		Join("LEFT", []string{"dts_student", "st"}, "st.id = r.student_id").
		Join("LEFT", []string{"dts_user", "u"}, "u.id = st.user_id").
		Join("LEFT", []string{"dts_school", "s"}, "s.id = r.school_id").
		And(filter).Limit(pageSize, offset).Desc("r.id").
		Find(&list)
	log.Println(err)
	count, err = engine.Table("dts_reported").Alias("r").
		Join("LEFT", []string{"dts_student", "st"}, "st.id = r.student_id").
		Join("LEFT", []string{"dts_user", "u"}, "u.id = st.user_id").
		Join("LEFT", []string{"dts_school", "s"}, "s.id = r.school_id").
		And(filter).Count()
	return
}

func (r *Reported) CheckExist(studentID, subject string) (has bool, list Reported, err error) {
	has, err = engine.Table("dts_reported").
		Where("student_id = ? and subject = ? ", studentID, subject).
		Get(&list)
	return
}

func (r *Reported) Delete() bool {
	if _, err := engine.Table("dts_reported").Where("id=?", r.Id).Delete(new(Reported)); err != nil {
		log.Println(err)
		return false
	}
	return true
}

type RecordPdf struct {
	Id      int
	FileUrl string
}

// GetLastPdfFile get last pdf url
func (m *Reported) GetLastPdfFile(school, student int) (recordPdf RecordPdf, err error) {
	if _, err = engine.Table("dts_reported").Alias("fr").Select("fr.id,pdf.file_url").
		Join("LEFT", []string{"dts_upload", "pdf"}, "fr.pdf_id=pdf.id").
		Where("fr.student_id=? and fr.school_id=?", student, school).OrderBy("fr.id desc").Limit(1).Get(&recordPdf); err != nil {
		return recordPdf, err
	}
	return recordPdf, nil
}
